package com.ideaaedi.springcloud.jd.user.controller.sys;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.ideaaedi.springcloud.jd.commonds.entity.Result;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.LoginRefreshTokenReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.LoginTokenAkSkReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.LoginTokenCheckReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.LoginTokenReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.LoginRefreshTokenRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.LoginTokenCheckRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.LoginTokenRespVO;
import com.ideaaedi.springcloud.jd.user.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统 - 登录
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.1.A
 */
@Slf4j
@RestController
@ApiSort(value = 1)
@Api(tags = "系统 - 登录")
@RequestMapping("/sys-login")
public class SysLoginController {
    
    @Resource
    private SysLoginService sysLoginService;
    
    /**
     * 普通登录获取token
     *
     * @param req 请求参数
     * @return token信息
     */
    @ApiOperationSupport(order = 1)
    @PostMapping("/basic/get-token")
    @ApiOperation(value = "普通登录获取token")
    public Result<LoginTokenRespVO> token(@Validated @RequestBody LoginTokenReqVO req) {
        return Result.success(sysLoginService.token(req));
    }
    
    /**
     * ak/sk登录获取token
     *
     * @param req 请求参数
     * @return token信息
     */
    @ApiOperationSupport(order = 2)
    @PostMapping("/ak-sk/get-token")
    @ApiOperation(value = "ak/sk登录获取token")
    public Result<LoginTokenRespVO> tokenByAkSk(@Validated @RequestBody LoginTokenAkSkReqVO req) {
        LoginTokenReqVO loginTokenReq = new LoginTokenReqVO();
        loginTokenReq.setAccountNo(req.getClientId());
        loginTokenReq.setPassword(req.getClientSecret());
        return Result.success(sysLoginService.token(loginTokenReq));
    }
    
    /**
     * 刷新token
     *
     * @param req 请求参数
     * @return 新的token信息
     */
    @ApiOperationSupport(order = 3)
    @PostMapping("/refresh-token")
    @ApiOperation(value = "刷新token")
    public Result<LoginRefreshTokenRespVO> refreshToken(@Validated @RequestBody LoginRefreshTokenReqVO req) {
        return Result.success(sysLoginService.refreshToken(req));
    }
    
    /**
     * 检查token激活状态
     *
     * @param req 请求参数
     *
     * @return token检查结果
     */
    @PostMapping("/check-token")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "检查token激活状态")
    public Result<LoginTokenCheckRespVO> checkToken(@Validated @RequestBody LoginTokenCheckReqVO req) {
        return Result.success(sysLoginService.checkToken(req));
    }
}
